package com.ppgames.gm.gmsystem.service;

import com.ppgames.gm.gmsystem.util.DBHelper;
import com.ppgames.gm.gmsystem.util.JsonHelper;
import com.ppgames.gm.gmsystem.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * @author Joeg
 * @email 840459673@qq.com
 * @date 2018/4/19
 */

@Service
public class ConfigService {

//    @Autowired
//    @Qualifier("entityManagerFactory")
    private EntityManager em;


    public boolean createConfig() {


        return false;
    }

    public boolean modifyConfig() {
        return false;
    }

    public boolean deleteConfig() {
        return false;
    }

    public Resp queryConfig(String name) {
        String sql = "SELECT name, config_json_str FROM  starwork_gm.t_config WHERE name=? ";
        Query query = em.createNativeQuery(sql);
        query.setParameter(1, name);
        List<Map> confList = DBHelper.getResultList(query, Map.class);
        if (confList == null || confList.size() != 1) {
            return Resp.customFail("1", "找不到或配置重复");
        }
        ServerConfigVO serverConfigVO = JsonHelper.toObject(confList.get(0).get("config_json_str"), ServerConfigVO.class);
        return Resp.success(serverConfigVO);
    }


    public static class ConfigVO {
        private Integer id;
        private String name;
        private String description;
        private String configJsonStr;
        private long updateTime;

        public ConfigVO(Integer id, String name, String description, String configJsonStr, long updateTime) {
            this.id = id;
            this.name = name;
            this.description = description;
            this.configJsonStr = configJsonStr;
            this.updateTime = updateTime;
        }

    }


    public static class ServerConfigVO {

        private String sid;     //服务器id
        private String host;    //服务器
        private String port;
        private String sname;
        private String ext1;
        private String ext2;
        private String ext3;

        public ServerConfigVO() {
        }

        public ServerConfigVO(String sid, String host, String port, String sname, String ext1, String ext2, String ext3) {
            this.sid = sid;
            this.host = host;
            this.port = port;
            this.sname = sname;
            this.ext1 = ext1;
            this.ext2 = ext2;
            this.ext3 = ext3;
        }

        public String getSid() {
            return sid;
        }

        public void setSid(String sid) {
            this.sid = sid;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public String getPort() {
            return port;
        }

        public void setPort(String port) {
            this.port = port;
        }

        public String getSname() {
            return sname;
        }

        public void setSname(String sname) {
            this.sname = sname;
        }

        public String getExt1() {
            return ext1;
        }

        public void setExt1(String ext1) {
            this.ext1 = ext1;
        }

        public String getExt2() {
            return ext2;
        }

        public void setExt2(String ext2) {
            this.ext2 = ext2;
        }

        public String getExt3() {
            return ext3;
        }

        public void setExt3(String ext3) {
            this.ext3 = ext3;
        }
    }

}
